package com.example.secondhandsysapi.Controller;

import com.example.secondhandsysapi.Entity.UserInfo;
import com.example.secondhandsysapi.Service.UserInfoService;
import com.example.secondhandsysapi.Vo.GenderCount;
import com.example.secondhandsysapi.Vo.MutlUser;
import com.example.secondhandsysapi.Vo.NewUserInfo;
import com.example.secondhandsysapi.util.FileUtil;
import com.example.secondhandsysapi.Vo.VGC;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value="user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    //http://localhost:8080/user/userinfo 得到所有用户的信息
    @GetMapping("/userinfo")
    public List<UserInfo> getUserInfoInPost(){
        return userInfoService.getUserDetailInfo();
    }

    @PostMapping("updateuserinfo")
    public Integer updateUserInfo(@RequestBody UserInfo userInfo){
        return userInfoService.updateUserInfo(userInfo);
    }


    //http://localhost:8080/user/login
    @PostMapping("/login")
    public Integer LoginBackend(@RequestBody UserInfo userInfo){
        Integer i = userInfoService.LoginBackend(userInfo);
        return i;
    }

    // http://localhost:8080/user/detailuserinfo
    //查询用户详细信息
    @GetMapping("/detailuserinfo")
    public List<UserInfo> getUserDetailInfo(@RequestParam(required = false) Integer user_id, @RequestParam(required = false) String user_name){
        List<UserInfo> result;
        if((user_id == null || user_id==0) && (user_name == null || user_name.isEmpty())){
            result = userInfoService.getUserDetailInfo();
        } else if (user_id == null || user_id==0) {
            result = userInfoService.getUserDetailInfoByName(user_name);
        }
        else if (user_name == null || user_name.isEmpty()){
            result = userInfoService.getUserDetailInfoByID1(user_id);
        }else { // 均不为空
            result = userInfoService.getUserDetailInfo1(user_id, user_name);
        }
        return result;
    }

// http://localhost:8080/user/showdetailuserinfo
@GetMapping("/showdetailuserinfo")
public UserInfo getUserDetailInfo(@RequestParam(required = false) Integer user_id){
    UserInfo result;
    result = userInfoService.getUserDetailInfoByID(user_id);
    return result;
}

    //根据日期来得到新用户的人数
    // http://localhost:8080/user/newuser
    @PostMapping("/newuser")
    public NewUserInfo getUserByDate(@RequestBody NewUserInfo newUserInfo) {
        NewUserInfo result = new NewUserInfo();
        List<Integer> newuser_count = new ArrayList<>();
        String step = newUserInfo.getStep();
        List<Date> registrationDates = new ArrayList<>();

        for (Date registration_date : newUserInfo.getRegistration_date()) {
            Integer count = 0;
            if (step.equals("day")) {
                count = userInfoService.findNewUser(registration_date);
            }
            newuser_count.add(count);
            registrationDates.add(registration_date);
        }

        result.setNewuser_count(newuser_count);
        result.setregistration_date(registrationDates);
        return result;
    }




    // http://localhost:8080/user/checkuser
    @GetMapping("checkuser")
    public List<UserInfo> checkUserByStatus(@RequestParam(required = false)Integer user_id,@RequestParam(required = false) String user_name){
        List<UserInfo> result;
        if ((user_id == null || user_id==0) && (user_name == null || user_name.isEmpty())) { // 全为空
            result = userInfoService.getAllUpdateUserInfo1();
        } else if (user_id == null || user_id==0) { // notice_type不为空，notice_theme为空
            result = userInfoService.getAllUpdateUserInfoByName(user_name);
        } else if (user_name == null || user_name.isEmpty()) { // notice_type为空，notice_theme不为空
            result = userInfoService.getAllUpdateUserInfoById(user_id);
        } else { // 均不为空
            result = userInfoService.getAllUpdateUserInfo(user_id, user_name);
        }
        return result;
    }

    // http://localhost:8080/user/page
    @GetMapping("page")
    public PageInfo<UserInfo> getUserInfo(@RequestParam Integer page,@RequestParam Integer size){
        PageInfo<UserInfo> i = userInfoService.getUserInfo(page,size);
        return i;
    }

    // http://localhost:8080/user/updateStatus/
    @PostMapping("updateStatus/{user_id}")
    public Integer updateStatus(@PathVariable Integer user_id){
        Integer i= userInfoService.updateStatus(user_id);
        return 1;
    }

    // http://localhost:8080/user/updateCheck_info
    @PostMapping("updateCheck_info")
    public Integer updateCheckInfo( String check_info,Integer user_id){
        Integer i =userInfoService.updateCheck_Info(check_info,user_id);
        return i;
    }

    // http://localhost:8080/user/updateMutlStatus
    @PostMapping("updateMutlStatus")
    public Integer updateMutlStatus(@RequestBody MutlUser mutlUser){
        Integer num = 0;
        for (Integer id : mutlUser.getUsers_id()) {
            userInfoService.updateStatus(id);
            num = num + 1;
        }
        return num;
    }

    //http:localhost:8080/user/updateMultCheck_info
    //多选的时候，按照给来的list做遍历，一个一个的做拆分出来执行updateCheck_Info方法
    @PostMapping("updateMultCheck_info")
    public Integer updateMultCheck_info(@RequestBody MutlUser mutlUser){
        Integer num = 0;
        for (String info: mutlUser.getChecks_info()) {
            for (Integer id : mutlUser.getUsers_id()) {
                userInfoService.updateCheck_Info(info, id);
                num = num + 1;
            }
        }
        return num;
    }

    //http://localhost:8080/user/upload
    @PostMapping("/upload")
    public String getImageFileName(MultipartFile file) {
        String oldFileName = file.getOriginalFilename();
        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf("."));
        String filePath = FileUtil.getUpLoadFilePath();
        System.out.println(typeName);
        String newFileName = System.currentTimeMillis() + typeName;
        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return newFileName;
    }


    //性别饼图
    //  http:localhost:8080/user/genderRatio
    @PostMapping("genderRatio")
    public GenderCount getGenderRatio() {
        List<VGC> genderCounts = userInfoService.getUserGenderCount();
        if (genderCounts == null) {
            throw new IllegalStateException("获取用户性别统计失败"); // 处理genderCounts为null的情况，例如返回一个默认的GenderCount对象或者抛出异常
        }
        GenderCount ret = new GenderCount();
        List<String> key = new ArrayList<>();
        List<Integer> vl = new ArrayList<>();
        for (VGC vgc : genderCounts) {
            key.add(vgc.getGender());
            vl.add(vgc.getVal());
        }
        ret.setGender(key);
        ret.setValue(vl);
        return ret;
    }
    @PostMapping("add")
    public Integer addUserInfo(@RequestBody UserInfo userInfo){
        Integer i = userInfoService.insertUserInfo(userInfo);
        return 1;
    }}
